package com.animal.controller;

import com.animal.dao.DailyManage;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class PageServlet extends HttpServlet{
    /**
     *
     */
    private static final long serialVersionUID = 1L;

    /**
     * Constructor of the object.
     */
    public PageServlet() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();

        String page = null;
        page = request.getParameter("datapage");
        int pagetot = 0;
        pagetot =  DailyManage.counts()/10+1;
        if(page == null || page=="")
            page = "0";
        int datapage = Integer.valueOf(page);
        if (datapage<0) {
            //out.print("<script>alert('叫你别点，你非要点');window.location='DailyFileManagement.jsp';</script>");
            datapage=datapage+1;
        }
        else if(datapage==pagetot){
            //out.print("<script>alert('叫你别点，你非要点');window.location='DailyFileManagement.jsp';</script>");
            datapage=datapage-1;
        }
            request.getSession().setAttribute("page", datapage);//当前页
            request.getRequestDispatcher("DailyFileManagement.jsp").forward(request, response);
            //response.sendRedirect("DailyFileManagement.jsp");
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        doGet(request,response);
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }
}
